Imports System.IO
Imports System
Imports System.Diagnostics
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class frmLOGIN
    'TODO:
    '4. Synchronize
    Dim IsControl As Boolean = False
    Dim index As Integer = 1
    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Dim dt As DataTable
        Dim arName As New ArrayList
        arName.Add("@UserName")
        arName.Add("@Password")
        Dim arVal As New ArrayList
        arVal.Add(txtUser.Text)
        arVal.Add(txtPassword.Text)
        dt = BLL.ObjectGetBy("USER_LOGIN", arName, arVal)

        If dt.Rows.Count > 0 Then

            PublicVariable.frmLogin = Me
            'Remember User, Password, Language
            Dim IniFile As String
            IniFile = Application.StartupPath + "\TOTALPOS.ini"
            If File.Exists(IniFile) Then
                Dim objIniFile As New INIClass(IniFile)
                objIniFile.WriteString("SYSTEM", "User", txtUser.Text)
                objIniFile.WriteString("SYSTEM", "Password", o3DES.DES.Encrypt(txtPassword.Text))
                objIniFile.WriteString("SYSTEM", "Language", PublicVariable.LangCode)
            End If
            'Dual Monitor
            
            PublicVariable.UserCode = dt.Rows(0).Item("UserCode").ToString
            PublicVariable.UserName = dt.Rows(0).Item("UserName").ToString
            PublicVariable.Password = txtPassword.Text
            PublicVariable.POSStoreCode = dt.Rows(0).Item("Store_Code").ToString
            oLogin.LoadAllConfiguration()
            If PublicVariable.DecimalNumber >= 0 Then
                PublicVariable.NumberFormatString = "{0:n" + CStr(PublicVariable.DecimalNumber) + "}"
            End If
            If PublicVariable.CustomerDisplayType = "2" Then
                PublicVariable.frmSecondScrn.pnBill.Closed = True
                PublicVariable.frmSecondScrn.txtMessage.Text = "WELCOME TO VINA POS SYSTEM"
                PublicVariable.frmSecondScrn.AxWindowsMediaPlayer1.settings.autoStart = True
                PublicVariable.frmSecondScrn.NewShow()
            End If
            

            Select Case PublicVariable.ApplicationType
                Case 0
                    Dim frm1 As New frmSIMPLE
                    Me.Hide()
                    frm1.ShowDialog()
                Case 1
                    Dim frm1 As New frmTABLEsimple
                    Me.Hide()
                    frm1.ShowDialog()
                Case 2
                    Dim frm1 As New frmORDER
                    Me.Hide()
                    frm1.ShowDialog()
                Case 3
                    Dim frm1 As New frmHOTEL
                    Me.Hide()
                    frm1.ShowDialog()
            End Select
            
        Else
            SystemFunction.ShowMessageForm(PublicFunction.GetLanguageMessage("frmLogin.LoginFailed"), "")
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

   

    Private Sub frmLOGIN_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        
        'Dim fileApp As New FileInfo("Updater.exe")
        'If (fileApp.Exists) Then
        '    Process.Start("Updater.exe")
        'End If

        Control.CheckForIllegalCrossThreadCalls = False
        'Dim LicenseFilePath As String
        'LicenseFilePath = Application.StartupPath + "\License.lic"
        'If File.Exists(LicenseFilePath) Then
        '    PublicVariable.IsLicense = o3DES.CheckLicense(LicenseFilePath)
        'Else
        '    PublicVariable.IsLicense = False
        'End If
        PublicVariable.IsLicense = True
        If Not PublicVariable.IsLicense Then
            gbLicense.Visible = True
        Else
            gbLicense.Visible = False
        End If
        'Check INI File
        Dim IniFile As String
        IniFile = Application.StartupPath + "\TOTALPOS.ini"
        If File.Exists(IniFile) Then

            Dim objIniFile As New INIClass(IniFile)
            Dim SQLServer As String = objIniFile.GetString("SQL", "Server", "local")
            Dim SQLPass As String = objIniFile.GetString("SQL", "SAPWD", "sa")
            Dim SQLDB As String = objIniFile.GetString("SQL", "SQLDB", "VINAPOS")
            Dim User As String = objIniFile.GetString("SYSTEM", "User", "")
            Dim Password As String = objIniFile.GetString("SYSTEM", "Password", "")
            PublicVariable.LangCode = objIniFile.GetString("SYSTEM", "Language", "")
            
            txtUser.Text = User
            txtPassword.Text = o3DES.DES.Decrypt(Password)
            PublicVariable.ConnectionString = "data source=" + SQLServer + ";initial catalog=" + SQLDB + ";persist security info=True;packet size=4096;user id=sa;password=" + o3DES.DES.Decrypt(SQLPass)
            PublicVariable.ReportConnectionString = SQLServer + ";" + SQLDB + ";sa;" + o3DES.DES.Decrypt(SQLPass) + ";"
            Thread_Init()
        Else
            SystemFunction.ShowMessageForm("MISSING CONFIG FILE!", "")
        End If

    End Sub
  

    Private Sub btnConf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConf.Click
        Dim frm As New frmPWD
        frm.ShowDialog()
    End Sub

    Private Sub UiButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UiButton1.Click, UiButton9.Click, UiButton7.Click, UiButton6.Click, UiButton5.Click, UiButton4.Click, UiButton3.Click, UiButton2.Click, UiButton11.Click, UiButton10.Click
        Select Case index
            Case 1
                txtUser.Focus()
            Case 2
                txtPassword.Focus()
        End Select
        SendKeys.Send(CType(sender, Janus.Windows.EditControls.UIButton).Text)
    End Sub

    Private Sub txtUser_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUser.Enter
        index = 1
    End Sub

    Private Sub txtPassword_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPassword.Enter
        index = 2
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Select Case index
            Case 1
                txtUser.Text = ""
            Case 2
                txtPassword.Text = ""
        End Select
    End Sub
    
    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            o3DES.GenerateREGFile(SaveFileDialog1.FileName)
            SystemFunction.ShowMessageForm("PLEASE SEND REGISTRATION FILE TO YOUR VENDOR!", "") '("Please send registration file to your vendor!")
        End If
    End Sub

    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim LicenseFilePath As String
            LicenseFilePath = Application.StartupPath + "\License.lic"
            File.Copy(OpenFileDialog1.FileName, Application.StartupPath + "\License.lic", True)
            PublicVariable.IsLicense = o3DES.CheckLicense(LicenseFilePath)
            If PublicVariable.IsLicense = True Then
                gbLicense.Visible = False
            End If
        End If
    End Sub
    Private Sub Thread_Init()
        Dim t As New Threading.Thread(AddressOf Init_App)
        t.IsBackground = True
        t.Priority = Threading.ThreadPriority.BelowNormal
        t.Start()
    End Sub
    Private Sub Init_App()
        CheckSQLConnection()
        LoadCrystal()
    End Sub
    Private Sub LoadCrystal()
        Dim crReport = New ReportDocument
        Try
            crReport.Load(Application.StartupPath + "\Report\rptBlank.rpt")
        Catch ex As Exception
            'MessageBox.Show("Load Blank Report Error!")
        End Try
    End Sub

    Private Sub CheckSQLConnection()
        PublicVariable.Connection = New SqlConnection
        PublicVariable.Connection.ConnectionString = PublicVariable.ConnectionString
        If PublicVariable.Connection.State = ConnectionState.Open Then
            PublicVariable.Connection.Close()
        End If
        Try
            PublicVariable.Connection.Open()
            btnLogin.Enabled = True
            gbLanguage.Enabled = True
            lblConnection.Text = "GOOD"
            PublicVariable.IsConnect = True
            PublicFunction.NewFormLoad(Me)

            Dim dt As DataTable
            Dim arName As New ArrayList
            arName.Add("@LangCode")
            Dim arVal As New ArrayList
            arVal.Add(PublicVariable.LangCode)
            dt = BLL.ObjectGetBy("LANG_LOADMESSAGE", arName, arVal)
            PublicVariable.LangMessage = dt

            oLogin.LoadReference()
            cbLang.DisplayMember = "LANG_NAME"
            cbLang.ValueMember = "LANG_CODE"
            dt = BLL.ObjectGetAll("LANGUAGE_LOAD")
            If dt.Rows.Count > 0 Then
                cbLang.DataSource = dt
                cbLang.SelectedIndex = 0
            End If
            'Dim versionInfo As FileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(sysdrv & "Program Files\D-Tools\daemon.exe")
            pnVersion.Text = "www.TOTAL-POS.net:" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() 'PublicVariable.AppVersion
        Catch ex As Exception
            lblConnection.Text = "FAILED"
            PublicVariable.IsConnect = False
        End Try
    End Sub
    

    Private Sub cbLang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbLang.Click
        cbLang.DroppedDown = True
    End Sub

    Private Sub cbLang_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbLang.SelectedValueChanged
        If PublicVariable.IsConnect Then
            PublicVariable.LangCode = cbLang.SelectedValue
            PublicFunction.NewFormLoad(Me)
            Dim dt As DataTable
            Dim arName As New ArrayList
            arName.Add("@LangCode")
            Dim arVal As New ArrayList
            arVal.Add(PublicVariable.LangCode)
            dt = BLL.ObjectGetBy("LANG_LOADMESSAGE", arName, arVal)
            PublicVariable.LangMessage = dt
        End If
    End Sub
End Class
